// Vec4.h: interface for the Vec4 class.
//
//////////////////////////////////////////////////////////////////////

#if !defined VEC4_H
#define VEC4_H

#include<iostream>
using std::ostream;
#include "Vec3.h"

class Vec4  
{
    public:
           
		float v[4]; // states are public for ease of access

		// Methods are defined here so that they are implicitly inlined

        Vec4() { v[0]=0.0f; v[1]=0.0f; v[2]=0.0f; v[3]=0.0f;}
        
        Vec4(float x, float y, float z, float w)
        {
            v[0]=x; v[1]=y; v[2]=z; v[3]=w;
        }

        Vec4(const Vec3& v3,float w)
        {
            v[0]=v3[0]; v[1]=v3[1]; v[2]=v3[2]; v[3]=w;
        }
            
		//operators
      
        inline bool operator == (const Vec4& v) const { 
				return v[0]==v.v[0] && v[1]==v.v[1] && v[2]==v.v[2] && v[3]==v.v[3]; }

        inline bool operator != (const Vec4& v) const { 
				return v[0]!=v.v[0] || v[1]!=v.v[1] || v[2]!=v.v[2] || v[3]!=v.v[3]; }

        inline bool operator < (const Vec4& v) const
        {
            if		(v[0]<v.v[0]) return true;
            else if (v[0]>v.v[0]) return false;
            else if (v[1]<v.v[1]) return true;
            else if (v[1]>v.v[1]) return false;
            else if (v[2]<v.v[2]) return true;
            else if (v[2]>v.v[2]) return false;
            else return (v[3]<v.v[3]);
        }

        inline float* ptr() { return v; }
        inline const float* ptr() const { return v; }

        inline void set( float x, float y, float z, float w) {
            v[0]=x; v[1]=y; v[2]=z; v[3]=w; }

        inline float& operator [] (unsigned int i) { return v[i]; }
        inline float  operator [] (unsigned int i) const { return v[i]; }

        inline float& x() { return v[0]; }
        inline float& y() { return v[1]; }
        inline float& z() { return v[2]; }
        inline float& w() { return v[3]; }

        inline float x() const { return v[0]; }
        inline float y() const { return v[1]; }
        inline float z() const { return v[2]; }
        inline float w() const { return v[3]; }

        inline bool valid() const { return !isNaN(); }
        inline bool isNaN() const { 
			return _isnan(v[0]) || _isnan(v[1]) || _isnan(v[2]) || _isnan(v[3]); }

        /// dot product
        inline float operator * (const Vec4& rhs) const {
            return v[0]*rhs.v[0] + v[1]*rhs.v[1] + v[2]*rhs.v[2] + v[3]*rhs.v[3]; }

        /// multiply by scalar
        inline Vec4 operator * (float rhs) const {
            return Vec4(v[0]*rhs, v[1]*rhs, v[2]*rhs, v[3]*rhs); }

        /// unary multiply by scalar
        inline Vec4& operator *= (float rhs) {
            v[0]*=rhs; v[1]*=rhs; v[2]*=rhs; v[3]*=rhs; return *this; }

        /// divide by scalar
        inline Vec4 operator / (float rhs) const {
            return Vec4(v[0]/rhs, v[1]/rhs, v[2]/rhs, v[3]/rhs); }

        /// unary divide by scalar
        inline Vec4& operator /= (float rhs) {
            v[0]/=rhs; v[1]/=rhs; v[2]/=rhs; v[3]/=rhs; return *this; }

        /// binary vector add
        inline Vec4 operator + (const Vec4& rhs) const {
            return Vec4(v[0]+rhs.v[0], v[1]+rhs.v[1],
		        v[2]+rhs.v[2], v[3]+rhs.v[3]); }

        /** unary vector add.  Slightly more efficient because no temporary
            intermediate object*/
        inline Vec4& operator += (const Vec4& rhs) {
            v[0] += rhs.v[0]; v[1] += rhs.v[1]; v[2] += rhs.v[2];
            v[3] += rhs.v[3]; return *this; }

        /// binary vector subtract
        inline Vec4 operator - (const Vec4& rhs) const {
            return Vec4(v[0]-rhs.v[0], v[1]-rhs.v[1],
		        v[2]-rhs.v[2], v[3]-rhs.v[3] ); }

        /// unary vector subtract
        inline Vec4& operator -= (const Vec4& rhs) {
            v[0]-=rhs.v[0]; v[1]-=rhs.v[1]; v[2]-=rhs.v[2];           
			v[3]-=rhs.v[3]; return *this; }

        /// negation operator.  Returns the negative of the Vec4
        inline const Vec4 operator - () const {
            return Vec4 (-v[0], -v[1], -v[2], -v[3]); }

        /// Length of the vector = sqrt( vec . vec )
        inline float length() const {
            return sqrtf( v[0]*v[0] + v[1]*v[1] + v[2]*v[2] + v[3]*v[3]);}

        /// Length squared of the vector = vec . vec
        inline float length2() const {
            return v[0]*v[0] + v[1]*v[1] + v[2]*v[2] + v[3]*v[3];}

        /** normalize the vector so that it has length unity
            returns the previous length of the vector*/
        inline float normalize()
        {
            float norm = Vec4::length();
            v[0] /= norm;
            v[1] /= norm;
            v[2] /= norm;
            v[3] /= norm;
            return( norm );
        }
        
        // to allow us to send our vector to the standard output stream cout
        friend inline ostream& operator << (ostream& output, const Vec4& vec)
        {
          output << "(" << vec.v[0] << "," << vec.v[1] << "," << vec.v[2] << "," << vec.v[3] << ")";
          return output;        
        }
};	// end of class Vec4


/** Compute the dot product of a (Vec3,1.0) and a Vec4.*/
inline float operator * (const Vec3& lhs,const Vec4& rhs)
{
    return lhs[0]*rhs[0]+lhs[1]*rhs[1]+lhs[2]*rhs[2]+rhs[3];
}

/** Compute the dot product of a Vec4 and a (Vec3,1.0).*/
inline float operator * (const Vec4& lhs,const Vec3& rhs)
{
    return lhs[0]*rhs[0]+lhs[1]*rhs[1]+lhs[2]*rhs[2]+lhs[3];
}

#endif // VEC_4
